package com.bree.erp.jsf.view;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.Project;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.PurchaseRequisitionService;
import com.bree.erp.service.PurchaseRequisitionWrapperService;
import com.bree.erp.service.wrapper.PurchaseRequisitionWrapperServiceBean;

@ManagedBean(name = "dtPurchaseRequisitionLazyView")
@ViewScoped
public class PurchaseRequisitionLazyView {

	private static Logger LOG = Logger.getLogger(PurchaseRequisitionLazyView.class);
			
	@EJB
	private PurchaseRequisitionService purchaseRequisitionService;
	
	@EJB
	private ProjectService projectService;

	private LazyDataModel<PurchaseRequisition> openLazyModel;
	private LazyDataModel<PurchaseRequisition> pendingLazyModel;
	private LazyDataModel<PurchaseRequisition> approvedLazyModel;
	private LazyDataModel<PurchaseRequisition> rejectedLazyModel;

	
	private PurchaseRequisition selectedItem;
	
	// input for creating purchase requisition
	private Long projectId;
	private String description;

	@PostConstruct
	public void init() {
		openLazyModel = new LazyBreeDataModel<PurchaseRequisition, PurchaseRequisitionWrapperService, Long>(
				new PurchaseRequisitionWrapperServiceBean(purchaseRequisitionService, Status.OPEN), 0L);

		pendingLazyModel = new LazyBreeDataModel<PurchaseRequisition, PurchaseRequisitionWrapperService, Long>(
				new PurchaseRequisitionWrapperServiceBean(purchaseRequisitionService, Status.PENDING), 0L);

		approvedLazyModel = new LazyBreeDataModel<PurchaseRequisition, PurchaseRequisitionWrapperService, Long>(
				new PurchaseRequisitionWrapperServiceBean(purchaseRequisitionService, Status.APPROVED), 0L);

		rejectedLazyModel = new LazyBreeDataModel<PurchaseRequisition, PurchaseRequisitionWrapperService, Long>(
				new PurchaseRequisitionWrapperServiceBean(purchaseRequisitionService, Status.REJECTED), 0L);
	}

	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("Item Selected",
				((PurchaseRequisition) event.getObject()).getId() + "");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public String createPurchaseRequistion() {
		
		return "edit";
	}
	
	public List<Project> getProjects() {
		return projectService.list();
	}
	/**
	 * @return the selectedItem
	 */
	public PurchaseRequisition getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(PurchaseRequisition selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the projectId
	 */
	public Long getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the openLazyModel
	 */
	public LazyDataModel<PurchaseRequisition> getOpenLazyModel() {
		
		LOG.debug("getOpenLazyModel:: " + openLazyModel);
		
		return openLazyModel;
	}

	/**
	 * @param openLazyModel the openLazyModel to set
	 */
	public void setOpenLazyModel(LazyDataModel<PurchaseRequisition> openLazyModel) {
		this.openLazyModel = openLazyModel;
	}

	/**
	 * @return the pendingLazyModel
	 */
	public LazyDataModel<PurchaseRequisition> getPendingLazyModel() {
		return pendingLazyModel;
	}

	/**
	 * @param pendingLazyModel the pendingLazyModel to set
	 */
	public void setPendingLazyModel(
			LazyDataModel<PurchaseRequisition> pendingLazyModel) {
		this.pendingLazyModel = pendingLazyModel;
	}

	/**
	 * @return the appprovedLazyModel
	 */
	public LazyDataModel<PurchaseRequisition> getApprovedLazyModel() {
		return approvedLazyModel;
	}

	/**
	 * @param appprovedLazyModel the appprovedLazyModel to set
	 */
	public void setApprovedLazyModel(
			LazyDataModel<PurchaseRequisition> appprovedLazyModel) {
		this.approvedLazyModel = appprovedLazyModel;
	}

	/**
	 * @return the rejectedLazyModel
	 */
	public LazyDataModel<PurchaseRequisition> getRejectedLazyModel() {
		return rejectedLazyModel;
	}

	/**
	 * @param rejectedLazyModel the rejectedLazyModel to set
	 */
	public void setRejectedLazyModel(
			LazyDataModel<PurchaseRequisition> rejectedLazyModel) {
		this.rejectedLazyModel = rejectedLazyModel;
	}
}
